package com.lab.book.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lab.book.common.bean.User;
import com.lab.book.common.dto.SearchUserDTO;
import com.lab.book.common.service.UserService;
import com.lab.book.server.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
@Service(timeout = 4000999)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> queryUserList(SearchUserDTO searchUserDTO) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("name", searchUserDTO.getName());
        return userMapper.selectList(qw);
    }

    public User registerUser(User user) throws Exception {
        try {
            int res = userMapper.insert(user);
            return user;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    public User existUser(String userName) throws Exception {
        try {
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("name", userName);
            return userMapper.selectOne(qw);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    @Override
    public User queryUserById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public Boolean updateUser(User user) throws Exception {
        try {
            int res = userMapper.updateById(user);
            if (res > 0) {
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }
}
